package com.imaker.frame

import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.imaker.frame.base.BaseActivity
import com.imaker.frame.databinding.ActivityMainBinding
import com.imaker.frame.ui.activity.HomeActivity
import com.imaker.frame.utils.ToastUtil
import com.imaker.webview.RouterPath

class SplashActivity : BaseActivity<ActivityMainBinding>(), View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.activity_main

    override fun init() {
        mBind.gotoWebview.setOnClickListener(this)

        requestPermissions(object : OnPermissionCallback {
            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                HomeActivity.start(this@SplashActivity)
                finish()
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                super.onDenied(permissions, doNotAskAgain)
                if (doNotAskAgain) {
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
                    XXPermissions.startPermissionActivity(this@SplashActivity, permissions)
                } else {
                    HomeActivity.start(this@SplashActivity)
                    ToastUtil.showMsg("获取读写权限失败")
                    finish()
                }
            }
        })

    }

    override fun observe() {
    }

    override fun onClick(v: View?) {
        if (v == mBind.gotoWebview) {
            ARouter.getInstance().build(RouterPath.WebView.WebViewActivity).navigation(this)
        }
    }


    /**
     * 请求权限
     */
    private fun requestPermissions(callback: OnPermissionCallback) {
        XXPermissions.with(this)
            .permission(
                arrayOf(
                    Permission.READ_MEDIA_IMAGES,
                    Permission.READ_MEDIA_AUDIO,
                    Permission.READ_MEDIA_VIDEO
                )
            )
            .request(callback)
    }
}